<template>
  <div class="dashboard">
    <header class="header">
      <div class="logo">
        <img src="@/assets/svgs/logo.svg" alt="Logo" class="logo-image">
        <span class="title">AIGC智育优学</span>
      </div>
      <div class="status">
        <span class="time">{{ formattedTime }}</span>
        <span class="class-status">{{ classStatus }}</span>
        <button v-if="classStatus !== '没有上课'" class="end-class-button">下课</button>
        <div class="user-info" @click="toggleDropdown">
          <img class="avatar" src="@/assets/images/rainbow.jpg" alt="User Avatar">
          <span class="username">当前用户</span>
          <img class="dropdown-arrow" src="@/assets/svgs/downnarrow.svg" alt="Dropdown Arrow">
          <div v-if="dropdownVisible" class="dropdown-menu">
            <p @click="navigateTo('profile')">个人信息</p>
            <p @click="navigateTo('logout')">退出登录</p>
          </div>
        </div>
      </div>
    </header>
    <div class="grid">
      <div class="grid-item" key="1" @click="navigateTo('ai')">
        <img class="icon" src="@/assets/svgs/robot.svg" alt="Icon">
        <span>AIGC智能上课助手</span>
      </div>
      <div class="grid-item" key="2" @click="navigateTo('chat')">
        <img class="icon" src="@/assets/svgs/chat.svg" alt="Icon">
        <span>AIGC智能聊天室</span>
      </div>
      <div class="grid-item" key="3" @click="navigateTo('record')">
        <img class="icon" src="@/assets/svgs/record.svg" alt="Icon">
        <span>课堂总结</span>
      </div>
      <div class="grid-item" key="4" @click="navigateTo('manager')">
        <img class="icon" src="@/assets/svgs/setting.svg" alt="Icon">
        <span>课程管理</span>
      </div>
      <div class="grid-item no-border" @click="showSchoolDialog">
        <img class="image" :src="selectedSchoolBadge" :alt="selectedSchoolName">
      </div>
      <div class="grid-item" key="5" @click="navigateTo('feedback')">
        <img class="icon" src="@/assets/svgs/feedback.svg" alt="Icon">
        <span>课堂反馈</span>
      </div>
      <div class="grid-item" key="6" @click="navigateTo('student')">
        <img class="icon" src="@/assets/svgs/students.svg" alt="Icon">
        <span>学生管理</span>
      </div>
      <div class="grid-item" key="7" @click="navigateTo('dialogue')">
        <img class="icon" src="@/assets/svgs/chat2.svg" alt="Icon">
        <span>对话数据</span>
      </div>
      <div class="grid-item" key="8" @click="navigateTo('interaction')">
        <img class="icon" src="@/assets/svgs/interaction.svg" alt="Icon">
        <span>互动数据</span>
      </div>
    </div>

    <!-- 添加学校选择对话框 -->
    <div v-if="dialogVisible" class="dialog-overlay" @click="closeDialog">
      <div class="dialog-content" @click.stop>
        <div class="dialog-header">
          <h3>选择学校</h3>
          <button class="close-button" @click="closeDialog">×</button>
        </div>
        <div class="school-grid-container">
          <div class="school-grid">
            <div v-for="school in schools" 
                 :key="school.name" 
                 class="school-option"
                 @click="selectSchool(school)">
              <img :src="school.badge" :alt="school.name">
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const currentTime = ref('');
const classStatus = ref('没有上课');
const formattedTime = ref('');
const dropdownVisible = ref(false);
const router = useRouter();
const dialogVisible = ref(false);

// 使用 import.meta.glob 动态导入所有学校徽章
const schoolBadges = import.meta.glob('@/assets/schoolbadge/*.svg', { eager: true });

// 处理徽章数据
const schools = Object.entries(schoolBadges).map(([path, module]) => {
  // 从路径中提取学校名称
  const name = path.split('/').pop()?.replace('.svg', '').toUpperCase();
  return {
    name,
    badge: module.default
  };
});

// 确保默认选中 GZU
const defaultSchool = schools.find(school => school.name === 'GZU') || schools[0];
const selectedSchoolBadge = ref(defaultSchool?.badge);
const selectedSchoolName = ref(defaultSchool?.name);

const toggleDropdown = () => {
  dropdownVisible.value = !dropdownVisible.value;
};

const navigateTo = (page) => {
  if (page === 'profile') {
    router.push('/profile');
  } else if (page === 'logout') {
    router.push('/logout');
  } else if (page === 'feedback') {
    router.push('/teacher/feedback/card');
  } else if (page === 'assignment') {
    router.push('/teacher/assignment/card');
  } else if (page === 'record') {
    router.push('/teacher/record/card');
  } else if (page === 'manager') {
    router.push('/teacher/manager');
  } else if (page === 'ai') {
    router.push('/ai');
  } else if (page === 'student'){
    router.push('/teacher/manager/student')
  } else if (page === 'chat'){
    router.push('/teacher/chat/index')
  } else if (page === 'dialogue'){
    router.push('/teacher/manager/dialogue')
  } else if (page === 'interaction'){
    router.push('/teacher/manager/interaction')
  }
};

const showSchoolDialog = () => {
  dialogVisible.value = true;
};

const closeDialog = () => {
  dialogVisible.value = false;
};

const selectSchool = (school) => {
  selectedSchoolBadge.value = school.badge;
  selectedSchoolName.value = school.name;
  closeDialog();
};

onMounted(() => {
  setInterval(() => {
    const now = new Date();
    const dateString = now.toLocaleDateString();
    const timeString = now.toLocaleTimeString();
    if (classStatus.value === '没有上课') {
      formattedTime.value = `${dateString} ${timeString}`;
    } else {
      formattedTime.value = `${classStatus.value} - ${dateString} ${timeString}`;
    }
  }, 1000);
});
</script>

<style>
.dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f0f0f0; /* 浅灰色背景 */
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #13227a;
  color: white;
  padding: 10px 20px;
}

.logo {
  display: flex;
  align-items: center;
  color: white;
  font-size: 18px;
  font-weight: bold;
}

.logo-image {
  width: 24px;
  height: 24px;
  margin-right: 10px;
}

.status {
  display: flex;
  align-items: center;
}

.time, .class-status, .username {
  margin-right: 20px;
}

.end-class-button {
  background-color: #13227a;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  position: relative;
  cursor: pointer;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 10px;
}

.dropdown-arrow {
  width: 16px;
  height: 16px;
  margin-left: 5px;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  color: black;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 10px;
  z-index: 1000;
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  padding: 20px;
  flex: 1;
  width: 1000px; /* 调整总宽度 */
  margin: 50px auto; /* 增加上下边距，保持水平居中 */
  justify-content: center; /* 网格水平居中 */
  align-content: center; /* 网格垂直居中 */
}

.grid-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: white;
  border-radius: 8px;
  width: 100%; /* 改为相对宽度 */
  height: 180px;
  cursor: pointer;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.grid-item.no-border {
  background-color: transparent;
  box-shadow: none;
  cursor: default;
}

.icon {
  width: 48px;
  height: 48px;
  margin-bottom: 10px;
}

.image {
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  padding: 20px;
  border-radius: 8px;
  width: 800px;
  height: 500px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.close-button {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #666;
  padding: 0 8px;
}

.close-button:hover {
  color: #000;
}

.school-grid-container {
  height: calc(100% - 60px);
  overflow-y: auto;
}

.school-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 15px;
  padding: 10px;
}

.school-option {
  padding: 10px;
  cursor: pointer;
  text-align: center;
}

.school-option img {
  width: 100px;
  height: 100px;
  object-fit: contain;
  transition: transform 0.2s;
}
</style>
